package hang.xipujewelWeb.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;

/**
 * 文件工具类
 * @author hang    
 * @date 2013-8-8 下午2:41:43        
 * @version V1.0
 */
public class FileUtil {

	/**
	 * @Title: 上传文件，传入文件的存储绝对路径和字节数组 
	 * @param @param file
	 * @param @param realPath
	 * @param @throws IOException    
	 * @return void
	 */
	public static void upload(byte[] file, String realPath) throws IOException {
		FileUtils.writeByteArrayToFile(new File(realPath), file);
	}

	/**
	 * @Title: 根据服务器存储目录和Picture对像中的文件信息获取文件 
	 * @param @param serverPath
	 * @param @param url
	 * @param @return
	 * @param @throws IOException    
	 * @return byte[]
	 */
	public static byte[] download(String serverPath, String url) throws IOException {
		return FileUtils.readFileToByteArray(new File(serverPath + url));
	}

	/**
	 * @Title: writeToFile 
	 * @param @param uploadedInputStream
	 * @param @param uploadedFileLocation    
	 * @return void
	 */
	public static void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
		try {
			OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
			int read = 0;
			byte[] bytes = new byte[1024];
			while ((read = uploadedInputStream.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
